﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinFormCenter.page {
    public partial class FormDelegate : Form {
        public FormDelegate() {
            InitializeComponent();
        }

        private void FormDelegate_Load(object sender, EventArgs e) {
           


        }

        delegate double Fun(double x);
        static double Linear(double x) {
            return x * 2 + 1;
        }
        class Rnd {
            readonly Random random = new();
            public double Num(double _) => random.NextDouble();
        }
        /// <summary>
        /// 积分计算
        /// </summary>
        /// <param name="f"></param>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <param name="eps"></param>
        /// <returns></returns>
        static double Integral(Fun f, double a, double b, double eps) {
            int n, k;
            double fa, fb, h, t1, p, s, x, t = 0;
            fa = f(a);
            fb = f(b);

            n = 1;
            h = b - a;
            t1 = h * (fa + fb) / 2.0;
            p = double.MaxValue;

            while (p >= eps) {
                s = 0.0;
                for (k = 0; k < n; k++) {

                    x = a + (k + 0.5) * h;
                    s += f(x);
                }
                t = (t1 + h * s) / 2.0;
                p = Math.Abs(t1 - t);
                t1 = t;
                n += n;
                h /= 2.0;
            }
            return t;

        }

      

        private void Button1_Click_1(object sender, EventArgs e) {
            var fun = new Fun(Math.Sin);
            double d = Integral(fun, 0, Math.PI / 2, 1e-4);
            Console.WriteLine(d);

            var fun2 = new Fun(Linear);
            double d2 = Integral(fun2, 0, 2, 1e-3);
            Console.WriteLine(d2);

            var rnd = new Rnd();
            double d3 = Integral(new Fun(rnd.Num), 0, 1, 0.01);
            Console.WriteLine(d3);
        }
        private void Button1_Click(object sender, EventArgs e) {
           
        }
        private void Form1_Paint(object sender, PaintEventArgs e) {
            Graphics g = e.Graphics;
            Pen pen = Pens.Blue;
            Fun[] funs = [
                new Fun(this.Square),
                new Fun(XPlus),
                new(Math.Cos),
                new(Math.Sqrt),
            ];
            foreach (var item in funs) {
                PlotFun(item, g,pen);
            }
        }

       

        private double Square(double x) {
            return Math.Sqrt(Math.Sqrt(x));
        }

        private static void PlotFun(Fun fun,Graphics g,Pen pen) {
            for(double x= 0; x < 10; x += 0.1) {
                double y= fun(x);
                var point = new Point((int)x * 20, (int)(200 - y * 30));
                g.DrawLine(pen,point,new Point(point.X+2,point.Y+2));
                Console.WriteLine(point);
            }
        }
        private static double XPlus(double x) {
            return Math.Sin(x)+Math.Cos(x*5)/5;
        }



    }
}
